﻿using Sidvall.FileSystemManager.Data;
using Sidvall.FileSystemManager.UI.Entities;
using System.Threading.Tasks;

namespace Sidvall.FileSystemManager.UI
{
	public partial class SystemContext
	{
		#region UserConfiguration

		private UserConfiguration _UserConfiguration;
		public UserConfiguration UserConfiguration
		{
			get
			{
				return _UserConfiguration;
			}
			private set
			{
				_UserConfiguration = value;
			}
		}

		#endregion

		#region LoadUserConfiguration

		public UserConfiguration LoadUserConfiguration(string content)
		{
			Sidvall.FileSystemManager.Net.Services.V1.UserConfiguration userConfiguration;
			UserConfiguration item;

			userConfiguration = Sidvall.FileSystemManager.SystemContext.Current.LoadUserConfiguration(content);
			item = new UserConfiguration();
			item.Import(userConfiguration, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
			item.AcceptChanges();
			this.UserConfiguration = item;

			return this.UserConfiguration;
		}
		public UserConfiguration LoadUserConfiguration(IUserConfiguration configuration)
		{
			Sidvall.FileSystemManager.Net.Services.V1.UserConfiguration dataItem;
			UserConfiguration item;

			dataItem = new Net.Services.V1.UserConfiguration();
			dataItem.Import(configuration, Sidvall.Data.CopySettings.FullCopyCurrent(Sidvall.Data.DataRowContextOption.None, null));
			dataItem = Sidvall.FileSystemManager.SystemContext.Current.LoadUserConfiguration(dataItem);
			item = new UserConfiguration();
			item.Import(dataItem, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
			item.AcceptChanges();
			this.UserConfiguration = item;

			return this.UserConfiguration;
		}

		#endregion
		#region LoadUserConfigurationAsync

		public async Task<UserConfiguration> LoadUserConfigurationAsync()
		{
			Sidvall.FileSystemManager.Net.Services.V1.UserConfiguration userConfiguration;
			UserConfiguration item;

			userConfiguration = await Sidvall.FileSystemManager.SystemContext.Current.LoadUserConfigurationAsync();
			item = new UserConfiguration();
			item.Import(userConfiguration, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
			item.AcceptChanges();
			this.UserConfiguration = item;

			return this.UserConfiguration;
		}

		#endregion
		#region SaveUserConfigurationAsync

		public async Task<UserConfiguration> SaveUserConfigurationAsync(IUserConfiguration configuration)
		{
			Sidvall.FileSystemManager.Net.Services.V1.UserConfiguration dataItem;
			UserConfiguration item;

			dataItem = new Net.Services.V1.UserConfiguration();
			dataItem.Import(configuration, Sidvall.Data.CopySettings.FullCopyCurrent(Sidvall.Data.DataRowContextOption.None, null));
			dataItem = await Sidvall.FileSystemManager.SystemContext.Current.SaveUserConfigurationAsync(dataItem);
			item = new UserConfiguration();
			item.Import(dataItem, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
			item.AcceptChanges();
			this.UserConfiguration = item;

			return this.UserConfiguration;
		}

		#endregion
	}
}